<?php
//include_once dirname(__FILE__) . '/profile.class.php';
//include_once dirname(__FILE__) . '/pp_config.php'; 

require_once 'main.class.php';  
require_once 'pp_config.php';  

abstract class IPNListener
{
    private function isVerifiedIPN()
    {
        $req = 'cmd=_notify-validate';
        foreach ($_POST as $key => $value)
        {
            $value = urlencode(stripslashes($value));
            $req .= "&$key=$value";
        }

        //Modificando la costante SIMULATION nel file di configurazione
        //è possibile passare dall'ambiente di simulazione a quello di produzione
        if(SIMULATION)
        {
            $url = SIMULATION_URL;
        }
        else
        {
            $url = PRODUCTION_URL;
        }

        $header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
        $header .= "Host: $url:443\r\n";
        $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
        $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

        $fp = fsockopen ("ssl://$url", 443, $errno, $errstr, 30);

        if (!$fp)
        {
			//$this->debugMail(__LINE__, "HTTP ERROR");
            // HTTP ERROR
            // $errstr - $errno
            $this->sendReport("HTTP ERROR");
            return FALSE;
        }
        else
        {
            fputs ($fp, $header . $req);
            while (!feof($fp))
            {
                $res = fgets ($fp, 1024);
                if (strcmp($res, "VERIFIED") == 0)
                {
                    fclose ($fp);
                    return TRUE;
                }
                else if (strcmp ($res, "INVALID") == 0)
                {
					//$this->debugMail(__LINE__, "IPN INVALID");
                    //se la procedura non è legittima invia un email all'amministratore
                    $this->sendReport("IPN INVALID");
                    fclose ($fp);
                    return FALSE;
                }
            }
        }
    }

    private function sendReport($problem="")
    {
        if(SIMULATION)
        {
            $add = "- SIMULAZIONE -";
        }
        else
        {
            $add = "";
        }
        //messaggio all'amministratore
        $subject = "$add Problema IPN";
        $message = "Si è verificato un problema nella seguente transazione:\r\n\r\n";
        $message .= "Nome: " . $_POST['first_name'] . ' ' . $_POST['last_name'] . "\r\n";
        $message .= "email: " . $_POST['payer_email'] . "\r\n";
        $message .= "id transazione: " . $_POST['txn_id'] . "\r\n";
        $message .= "oggetto: " . $_POST['transaction_subject'] . "\r\n";
        $message .= "problema: " . $problem;

        mail(ADMIN_MAIL,$subject,$message,"From: " . NO_REPLY);
        return;
    }

    private function isCompleted()
    {
        if(trim($_POST['payment_status']) === "Completed")
        {
            return TRUE;
        }
        $this->sendReport("Payment_status: ".$_POST['payment_status']);
        return FALSE;
    }

    private function isPrimaryPayPalEmail()
    {

        if(SIMULATION)
        {
            $email = PRIMARY_SANDBOX_EMAIL;
        }
        else
        {
            $email = PRIMARY_PAYPAL_EMAIL;
        }

        if(trim($_POST['receiver_email']) === $email)
        {
            return TRUE;
        }
        $this->sendReport("PrimaryPayPalEmail non corretto!");
        return FALSE;
    }

    abstract protected function isVerifiedAmmount();
    abstract protected function isNotProcessed();

    protected function isReadyTransaction()
    {
        if($this->isVerifiedIPN() AND
           $this->isPrimaryPayPalEmail() AND
           $this->isNotProcessed() AND
           $this->isVerifiedAmmount() AND
           $this->isCompleted())
           {
			   $this->debugMail(__LINE__, "TUTTO OK");
               return TRUE;
           }
		   $this->debugMail(__LINE__, "CONTROLLI KO");
           return FALSE;
    }
}
?>
